/**
 * 粒子函数：实体粒子特效
 * 这个函数使得实体呈现向上的迸发粒子
 * @param id 实体的id，为字符串格式
 */

async function particle(id: string) { // 定义particle()函数
    let block = world.querySelector(`#${id}`) as GameEntity; // 定义block实体，方便调用
    if (block.player) { block.player.emissive = 1 } // 玩家发光
    block.meshEmissive = 1 //实体发光
    Object.assign(block, {
        particleRate: 400, // 粒子发射速率：每秒生成120个粒子，营造密集且爆发感
        particleSize: [2, 5, 8, 5, 0], // 粒子大小变化
        particleLifetime: 4.5, // 粒子生命周期：每个粒子存在4.5秒，确保效果不超过5秒
        particleVelocity: new GameVector3(0, 35, 0), // 粒子初始速度
        particleVelocitySpread: new GameVector3(12, 7, 12), // 粒子初始速度的随机范围
        particleColor: [
            new GameRGBColor(2.0, 2.0, 2.0),
            new GameRGBColor(0.53, 0.87, 0.94),
            new GameRGBColor(0.8, 1.0, 1.5),
            new GameRGBColor(0.4, 0.6, 1.0),
            new GameRGBColor(0.2, 0.3, 0.5)
        ],
        // particleDamping: 0.9,
        // particleAcceleration: new GameVector3(0, -9.8, 0),
        // particleNoise: 5,
        // particleNoiseFrequency: 5,
    })
    setTimeout(() => {
        Object.assign(block, { particleRate: 0 });// 关闭粒子
        block.meshEmissive = 0 // 停止发光
        if (block.player) { block.player.emissive = 0 } // 玩家停止发光
    }, 500); // 五秒后
}

world.onPlayerJoin(async ({ entity }) => { // 当玩家进入地图
    entity.holdTime = 0; // 玩家初始加载时间为0
});

async function AreaLeave(e: GameEntity) {
    e.holdTime = 0
    e.player?.directMessage(`已经退出传送区域~`)
};

setInterval(() => {
    world.querySelectorAll('player').forEach(async (e) => {
        if (e.hasTag('showArea') || e.hasTag('liuArea') || e.hasTag('SISArea') || e.hasTag('DuckArea') || e.hasTag('PandaArea')) {
            if (!(e.hasTag('showArea') || e.hasTag('liuArea') || e.hasTag('SISArea') || e.hasTag('DuckArea') || e.hasTag('PandaArea'))) return
            e.holdTime += 1;
            if (e.holdTime >= 10) {
                e.player.link(e.goingURL, { isConfirm: false, isNewTab: false })
                return
            }
            let restTime = 10 - e.holdTime
            remoteChannel.sendClientEvent(e, restTime)
            // e.player.directMessage(`即将传送，还有${restTime}秒进入${e.chooseGame}`)
        }
    });
}, 1000);

//------展示------//
const showArea = world.addZone({
    selector: 'player',
    bounds: new GameBounds3(
        new GameVector3(119, 19, 137,),
        new GameVector3(127, 21, 147,)
    ),
});

showArea.onEnter(async ({ entity }: { entity: any }) => {
    remoteChannel.sendClientEvent(entity, 'showAreaEnter');
    // console.log('showAreaEnter');
    entity.addTag('showArea');
    entity.chooseGame = '「萌新创作者急救营」官方QQ群';
    entity.goingURL = 'https://qm.qq.com/q/J1UWxlwr6O';
    particle('中心-大块')
});

showArea.onLeave(async ({ entity }: { entity: any }) => {
    remoteChannel.sendClientEvent(entity, 'AreaLeave');
    // console.log('showAreaLeave');
    entity.removeTag('showArea');
    AreaLeave(entity)
    entity.holdTime = 0;
});
//------展示------//

//------流璃awa------//
const LiuArea = world.addZone({
    selector: 'player',
    bounds: new GameBounds3(
        new GameVector3(99, 19, 140,),
        new GameVector3(103, 29, 144,)
    ),
});

LiuArea.onEnter(async ({ entity }: { entity: any }) => {
    remoteChannel.sendClientEvent(entity, 'liuAreaEnter');
    entity.chooseGame = '@流璃awa 的地图'
    // console.log('liuAreaEnter');
    entity.addTag('liuArea');
    entity.goingURL = 'https://dao3.fun/play/6c50a04e09a8ac371c13'
    particle('流-大块')
});

LiuArea.onLeave(async ({ entity }: { entity: any }) => {
    remoteChannel.sendClientEvent(entity, 'AreaLeave');
    // console.log('liuAreaLeave');
    entity.removeTag('liuArea');
    AreaLeave(entity)
    entity.holdTime = 0;
});
//------流璃awa------//

//------Star_Ink_sans------//
const SISArea = world.addZone({
    selector: 'player',
    bounds: new GameBounds3(
        new GameVector3(121, 19, 120,),
        new GameVector3(125, 29, 124,)
    ),
});

SISArea.onEnter(async ({ entity }: { entity: any }) => {
    remoteChannel.sendClientEvent(entity, 'SISAreaEnter');
    entity.chooseGame = '@Star_Ink_sans 的地图'
    // console.log('SISAreaEnter');
    entity.addTag('SISArea');
    entity.goingURL = 'https://dao3.fun/play/b8f165692508a36153f9'
    particle('SIS')
});

SISArea.onLeave(async ({ entity }: { entity: any }) => {
    remoteChannel.sendClientEvent(entity, 'AreaLeave');
    // console.log('SISAreaLeave');
    entity.removeTag('SISArea');
    AreaLeave(entity)
    entity.holdTime = 0;
});
//------Star_Ink_sans------//

//------鹅玎鸭------//
const DuckArea = world.addZone({
    selector: 'player',
    bounds: new GameBounds3(
        new GameVector3(149, 19, 140,),
        new GameVector3(153, 29, 144,)
    ),
});

DuckArea.onEnter(async ({ entity }: { entity: any }) => {
    remoteChannel.sendClientEvent(entity, 'DuckAreaEnter');
    entity.chooseGame = '@鹅玎鸭 的地图'
    // console.log('DuckAreaEnter');
    entity.addTag('DuckArea');
    entity.goingURL = 'https://dao3.fun/play/a59f5b9733205b2fce97'
    particle('Duck')
});

DuckArea.onLeave(async ({ entity }: { entity: any }) => {
    remoteChannel.sendClientEvent(entity, 'AreaLeave');
    // console.log('DuckAreaLeave');
    entity.removeTag('DuckArea');
    AreaLeave(entity)
    entity.holdTime = 0;
});
//------鹅玎鸭------//

//------黑客熊猫------//
const PandaArea = world.addZone({
    selector: 'player',
    bounds: new GameBounds3(
        new GameVector3(121, 19, 168,),
        new GameVector3(125, 29, 172,)
    ),
});

PandaArea.onEnter(async ({ entity }: { entity: any }) => {
    remoteChannel.sendClientEvent(entity, 'PandaAreaEnter');
    entity.chooseGame = '@黑客熊猫 的地图'
    // console.log('PandaAreaEnter');
    entity.addTag('PandaArea');
    entity.goingURL = 'https://dao3.fun/play/e17e8749e59a5b72c214'
    particle('Panda')
});

PandaArea.onLeave(async ({ entity }: { entity: any }) => {
    remoteChannel.sendClientEvent(entity, 'AreaLeave');
    // console.log('PandaAreaLeave');
    entity.removeTag('PandaArea');
    AreaLeave(entity)
    entity.holdTime = 0;
});
//------黑客熊猫------//